function saveImageToPhotosAlbum(option) {
  return new Promise((resolve, reject) => {
    wx.saveImageToPhotosAlbum({
      ...option,
      success: resolve,
      fail: reject,
    })
  })
}
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: "",
    maskey: true,
    maskbtn_key: true,
    tit: "",
    doctor: [],
    zixun: [],
    answer: [],
    page: 0,
    pagesize: 10,
    btns: [{
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon1.png?sign=f7940ecce77ef951569666ee63e31018&t=1585206589",
        texts: "简介",
        ids: "a1"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon2.png?sign=303c39f55305b63204368eaa4169112c&t=1585206619",
        texts: "症状",
        ids: "a2"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon3.png?sign=5b20de734027ce99f44bce7582f783c5&t=1585206647",
        texts: "病因",
        ids: "a3"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon4.png?sign=ba1da0ee474ad6e3428646737fcf380f&t=1585206675",
        texts: "诊断",
        ids: "a4"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon5.png?sign=abe58590be0f11e02e089ea0bb3ad159&t=1585206696",
        texts: "治疗",
        ids: "a5"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon5.png?sign=abe58590be0f11e02e089ea0bb3ad159&t=1585206696",
        texts: "预防",
        ids: "a6"
      },
      {
        imgs: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/icon6.png?sign=dec672a4855a5df3d919b7351d22955d&t=1585206721",
        texts: "更多信息",
        ids: "a7"
      },
    ],
    tabs: ["推荐医生", "公开问题", "科普文章"],
    tabcurrent: 0,
    fixeds: false,
  },
  Tapitem: function (e) {
    var name = e.currentTarget.dataset.name;
    var id = this.data.id;
    var title = this.data.title;
    var ids = e.currentTarget.dataset.ids;
    console.log(ids)
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/info/info?title=' + title + '&id=' + id + '&&name=' + name + '&ids=' + ids + '&title=' + title,
    })

  },
  // 取消遮罩层
  mask_key: function () {
    this.setData({
      maskey: true
    })
  },
  // 出现遮罩层
  hasmask: function (e) {
    var that = this
    var id = that.data.id
    app.util.request({
      'url': 'entry/wxapp/answer.generate',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          img: res.data
        })
        // console.log(that.data.img)
      },
      fail: function (err) {
        console.log(err)
      },
    })
    that.setData({
      maskey: false
    })
  },
  // 下载图片
  maskbtn: function () {
    // 保存到相册
    let that = this
    const imageFile = that.data.img
    wx.showLoading({
      title: '图片保存中...'
    })
    wx.downloadFile({
      url: imageFile,
      success: function (res) {
        // 图片保存到本地
        console.log(res.tempFilePath)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.hideLoading()
            wx.showModal({
              title: '图片已存入相册',
              content: '好东西要分享，发给朋友们看看。',
              showCancel: false
            })
          },
          fail: function (err) {
            console.log(err)
            if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
              console.log('22222222222222222222222222222222=============================')
              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log('settingdata', settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击按钮即可保存',
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册！',
                          showCancel: false
                        })
                      }
                    },
                    fail(failData) {
                      console.log('failData', failData)
                    },
                    complete(finishData) {
                      console.log('finishData', finishData)
                    }
                  })
                }
              })
            }
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    })
  },



  navicate() {
    var that = this
    wx.reLaunch({
      url: '/hyb_yl/tabBar/index/index',
    })
  },
  tabclick(e) {
    this.setData({
      tabcurrent: e.currentTarget.dataset.dex
    })
    var that = this;
    that.setData({
      page: 0,
      doctor: [],
      zixun: [],
      answer: [],
    })
    var tabcurrent = e.currentTarget.dataset.dex;
    if (tabcurrent == '0') {
      that.getdoctor();
    } else if (tabcurrent == '1') {
      that.getAnswer();
    } else if (tabcurrent == '2') {
      that.getzixun();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.scene) {
      var info = decodeURIComponent(options.scene)
      var id = res;
      that.setData({
        id: id
      })
    } else {
      wx.setNavigationBarTitle({
        title: options.title,
      })
      var id = options.id
      app.util.request({
        'url': "entry/wxapp/answer.tank_detail",
        data: {
          id: id
        },
        success: function (res) {
          console.log(res.data.content)
          that.setData({
            content: res.data.content
          })
        }
      })
      that.setData({
        title: options.title,
        id: id
      })
    }

    that.getDetail();
    that.getdoctor();
  },
  // 获取详细信息
  getDetail: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/answer.tank_detail",
      data: {
        id: that.data.id
      },
      success: function (res) {
        that.setData({
          info: res.data
        })
        wx.setNavigationBarTitle({
          title: res.data.title,
        })
      }
    })
  },
  getdoctor: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/answer.getdocor",
      data: {
        page: that.data.page,
        pagesize: that.data.pagesize,
        id: that.data.id
      },
      success: function (res) {
        var page = that.data.page + 1;
        that.setData({
          doctor: that.data.doctor.concat(res.data),
          page: page
        });
      }

    })
  },
  getzixun: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/answer.getzixun",
      data: {
        page: that.data.page,
        pagesize: that.data.pagesize,
        id: that.data.id
      },
      success: function (res) {
        var page = that.data.page + 1;
        that.setData({
          zixun: that.data.zixun.concat(res.data),
          page: page
        });
      }

    })
  },
  getAnswer: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/answer.getAnswer",
      data: {
        page: that.data.page,
        pagesize: that.data.pagesize,
        id: that.data.id
      },
      success: function (res) {
        var page = that.data.page + 1;
        that.setData({
          answer: that.data.answer.concat(res.data),
          page: page
        });
      }

    })
  },
  onPageScroll: function (e) {
    let query = wx.createSelectorQuery();
    query.select('.tabs').boundingClientRect(rect => {
      // let height = rect.top;
      // console.log(height)
      // if (height < 0){
      //   this.setData({
      //     fixeds:true
      //   })
      // }else{
      //   this.setData({
      //     fixeds: false
      //   })
      // }
    }).exec();
  },
  navigator(e) {
    var zid = e.currentTarget.dataset.zid;

    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid=' + zid,
    })
  },
  handleNavigateDetail(e) {
    var id = e.currentTarget.dataset.id;
    var p_id = e.currentTarget.dataset.p_id;
    var zid = e.currentTarget.dataset.zid;
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq?id=' + id + '&p_id=' + p_id + '&zid=' + zid,
    })
  },
  lookdetail() {
    var id = this.data.id;
    var title = this.data.title;
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/info/info?title=' + title + '&id=' + id + '&ids=a7' + 'names=简介',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var tabcurrent = that.data.tabcurrent;
    if (tabcurrent == '0') {
      that.getdoctor();
    } else if (tabcurrent == '1') {
      that.getAnswer();
    } else if (tabcurrent == '2') {
      that.getzixun();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})